//
//  GFMultipleColumnsTVCell.m
//  UITableView多列展示
//
//  Created by 高得华 on 16/9/2.
//  Copyright © 2016年 GaoFei. All rights reserved.
//

#define kJianXi 15.0f
#define kViewWidth  [UIScreen mainScreen].bounds.size.width
#define kViewHeight [UIScreen mainScreen].bounds.size.height
#define kClickBtnHeight 36

#import "GFMultipleColumnsTVCell.h"

@interface GFMultipleColumnsTVCell ()

@property (nonatomic,assign) NSInteger columns;

@end

@implementation GFMultipleColumnsTVCell

-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        [self loadCreateViewLayout];
        
        
    }
    return self;
}

-(instancetype)init{
    if ([super init]) {
        [self loadCreateViewLayout];
    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];
    [self loadCreateViewLayout];
}

//设置页面布局
- (void) loadCreateViewLayout{
    
    self.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
    CGRect mainScreen = [UIScreen mainScreen].bounds;
    if (mainScreen.size.width == 320) {
        self.columns = 9;
    }else{
        self.columns = 4;
    }
}
-(void)setDataArrayCount:(NSInteger)dataArrayCount{
    _dataArrayCount = dataArrayCount;
    self.cellHeight = [self heightForCount:_dataArrayCount];
}
-(void)setIndexPath:(NSIndexPath *)indexPath{
    _indexPath = indexPath;
}
-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    for (UIView *view in self.subviews) {
        
        [view removeFromSuperview];
    }
    [self loadCreateScratchableLatex];
}

//创建九宫格---
- (void)loadCreateScratchableLatex {
    //热门视频导航
    NSDictionary *hotCityDic = [[NSUserDefaults standardUserDefaults]objectForKey:@"hotCityArr"];
    //热门城市数量
    NSArray *hotCityCount = [hotCityDic valueForKey:@"address"];
    //热门城市名称
    NSArray *hotCityName = [hotCityCount valueForKey:@"title"];
    //热门城市id
    NSArray *cityID = [hotCityCount valueForKey:@"id"];
    //第一组数据
if (_indexPath.section==0) {
    for (NSInteger i = 0; i < hotCityCount.count ; i++) {
        UIButton * selectBtn = [[UIButton alloc] initWithFrame:[self frameForItemIndex:i]];
        selectBtn.backgroundColor = [UIColor grayColor];
        [selectBtn addTarget:self action:@selector(clickBtnAct:) forControlEvents:UIControlEventTouchUpInside];
        NSString *btnTitle = hotCityName[i];
        [selectBtn setTitle:btnTitle forState:UIControlStateNormal];
        selectBtn.titleLabel.font = [UIFont systemFontOfSize:9];
        [self addSubview:selectBtn];
        
        selectBtn.tag = [cityID[i] integerValue];
    }
}
                //第二类标题
                NSArray *video_class = [hotCityDic valueForKey:@"video_class"];
                //标题名称
                NSArray *title = [video_class valueForKey:@"title"];
                //id
                NSArray *video_classID = [video_class valueForKey:@"id"];
                //第二组数据
                if (_indexPath.section==1) {
                    for (NSInteger x = 0; x<video_class.count; x++) {
                        UIButton * selectBtn = [[UIButton alloc] initWithFrame:[self frameForItemIndex:x]];
                        selectBtn.backgroundColor = [UIColor grayColor];
                        [selectBtn addTarget:self action:@selector(clickBtnAct:) forControlEvents:UIControlEventTouchUpInside];
                        NSString *btnTitle = title[x];
                        [selectBtn setTitle:btnTitle forState:UIControlStateNormal];
                        [self addSubview:selectBtn];
                        selectBtn.titleLabel.font = [UIFont systemFontOfSize:9];
                        selectBtn.tag = [video_classID[x] integerValue];
        
                    }
                }
                //  第三组数据
                if (_indexPath.section==2) {
                    
                    //第二类标题
                    NSArray *video_class = [hotCityDic valueForKey:@"video_class"];
                    //标题名称
                    NSArray *title = [video_class valueForKey:@"title"];
                    //id
                    NSArray *video_classID = [video_class valueForKey:@"id"];
                    for (NSInteger y = 0; y<title.count; y++) {
                        UIButton * selectBtn = [[UIButton alloc] initWithFrame:[self frameForItemIndex:y]];
                        NSString *btnTitle = title[y];
                        selectBtn.backgroundColor = [UIColor grayColor];
                        [selectBtn addTarget:self action:@selector(clickBtnAct:) forControlEvents:UIControlEventTouchUpInside];
                       // NSArray *sectionTwoArr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0"];
                        //NSString *btnTitle = sectionTwoArr[y];
                        [selectBtn setTitle:btnTitle forState:UIControlStateNormal];
                        selectBtn.tag = [video_classID[y] integerValue];

                        [self addSubview:selectBtn];
                        selectBtn.tag = y+100;
                    }
                }
    }
//}
//btn的点击响应事件
- (void)clickBtnAct:(UIButton *)sender {
    NSLog(@"sende.ID ==%ld",sender.tag);
    
    // self.ReturnClickItemIndex(self.indexPath, sender.tag - 100);
}

//计算每个UIButton的frame
-(CGRect)frameForItemIndex:(NSInteger)count{
    
    //每个图片的宽度
    CGFloat itemW = (kViewWidth - (self.columns+1)*kJianXi)/self.columns;
    //计算xy轴的坐标
    CGFloat x = count%self.columns*itemW +kJianXi *(count%self.columns+1);
    CGFloat y = count/self.columns*kClickBtnHeight +kJianXi *(count/self.columns+1);
    
    return CGRectMake(x, y, itemW, kClickBtnHeight);
}

//根据数据计算高度
-(CGFloat)heightForCount:(NSInteger)count{
    
    //计算行数
    long row = count/self.columns;
    
    if (count%self.columns != 0) {
        
        row++;
    }
    
    //每个图片的宽度
    //    CGFloat itemW = (kViewWidth - (self.columns+1)*kJianXi)/self.columns;
    CGFloat height = kClickBtnHeight * row +kJianXi*(row+1);
    
    return height;
}

@end
